package com.ssyan.prototype.deepclone;

/**
 * @author ssyan
 * @Description 深拷贝的两种方式：
 *              1.重写clone方法分别处理基本类型和引用类型（优点：效率高，缺点：引用链长时代码编写负责）
 *              2.使用序列化和反序列化的方式（优点：无视引用链的深浅可以完全复制，缺点：复杂对象使用流影响效率）
 * @create 2021/8/28 1:54
 * @Version 1.0
 */
public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        DeepPrototype deepPrototype = new DeepPrototype();
        deepPrototype.name = "宋江";
        deepPrototype.deepCloneableTarget = new DeepCloneableTarget("大牛","大牛的类");

        //方式一完成深拷贝
//        DeepPrototype deepPrototype2 = deepPrototype.clone();
//
//        System.out.println("deepPrototype.name=" + deepPrototype.name + " deepPrototype.deepCloneableTarget=" + deepPrototype.deepCloneableTarget.hashCode());
//        System.out.println("deepPrototype2.name=" + deepPrototype2.name + " deepPrototype2.deepCloneableTarget=" + deepPrototype2.deepCloneableTarget.hashCode());

        //方式二完成深拷贝
        DeepPrototype deepPrototype2 = deepPrototype.deepClone();

        System.out.println("deepPrototype.name=" + deepPrototype.name + " deepPrototype.deepCloneableTarget=" + deepPrototype.deepCloneableTarget.hashCode());
        System.out.println("deepPrototype2.name=" + deepPrototype2.name + " deepPrototype2.deepCloneableTarget=" + deepPrototype2.deepCloneableTarget.hashCode());
    }
}
